package wicket.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import wicket.Person;
import wicket.PersonDAO;
import wicket.util.HibernateUtil;

public class HbPersonDAO implements PersonDAO {

	@Override
	public Person getPerson(Person person) throws HibernateException {
		Person p = null;
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List result = session.createQuery("from Person p where name='" + person.getName() + "' and password='" + person.getPassword() + "'").list();
		
		if (result.size() > 0)
			p = (Person)result.get(0);
		
		session.getTransaction().commit();
		
		return p;
	}
	
	@Override
	public void add(Person person) throws HibernateException{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(person);
		session.getTransaction().commit();
	}

}
